package com.deparse.mydialog.listview;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.deparse.mydialog.R;
import com.deparse.mydialog.common.BaseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/7/20.
 */
public class SwipeRefreshLayout extends BaseActivity implements android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener {
    private PullRefreshListView pullList;
    private ListView listView;
    private AdapterList adapterList;
    private List<String> listData = new ArrayList<>();
    private LayoutInflater layoutInflater;
    private android.support.v4.widget.SwipeRefreshLayout refresh;

    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 0:
                    listData.clear();
                    listData = getData(0, 20);
                    adapterList.notifyDataSetChanged();

                    refresh.setRefreshing(false);
                    break;
                case 1:
                    listData.add("ListView数据的分批加载");
                    listData.add("ListView数据的分批加载");
                    listData.add("ListView数据的分批加载");
                    listData.add("ListView数据的分批加载");
                    listData.add("ListView数据的分批加载");
                    // 告诉ListView数据已经发生改变，要求ListView更新界面显示
                    adapterList.notifyDataSetChanged();
                    if (listView.getFooterViewsCount() > 0) { // 如果有底部视图
                        listView.removeFooterView(footer);
                    }
                    pullList.loadFinish(); // 加载完成
                    break;

            }
        }


    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refresh);
        layoutInflater = LayoutInflater.from(this);
        pullList = (PullRefreshListView) findViewById(R.id.pullList);
        listView = pullList.getListView();
        refresh = pullList.getRefresh();
        footer = pullList.getFooterVIew();


        refresh.setOnRefreshListener(this);

        listData = getData(0, 20);
        adapterList = new AdapterList();
        listView.setAdapter(adapterList);
        listView.removeFooterView(footer);

        pullList.setOnLoadListener(new PullRefreshListView.OnLoadListener() {
            @Override
            public void onLoadListener() {
                    mHandler.sendEmptyMessageDelayed(1, 1000);


            }
        });

    }

    @Override
    public void onRefresh() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(refresh, "translationY", 0, 300, 1);
        animator.setDuration(4000);
        animator.start();
        mHandler.sendEmptyMessageDelayed(0, 4000);
    }

    private int number = 10; // 每次获取多少条数据
    private int maxpage = 5; // 总共有多少页
    View footer;


    public List<String> getData(int offset, int maxResult) { // 分页limit
        // 0,20
        List<String> data = new ArrayList<String>();
        for (int i = 0; i < 10; i++) {
            data.add("ListView数据的分批加载" + i);
        }
        return data;

    }

    private class AdapterList extends BaseAdapter {

        @Override
        public int getCount() {
            return listData.size();
        }

        @Override
        public Object getItem(int position) {
            return listData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = layoutInflater.inflate(R.layout.item_main_list, null);
            TextView activity_name = (TextView) convertView.findViewById(R.id.activity_name);
            activity_name.setText(listData.get(position));
            return convertView;
        }
    }
}
